package com.ant.product.filter;

import com.alibaba.fastjson.JSONObject;
import com.ant.product.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class LogScore {

    @Autowired
//    private StudentService service;
    //切点
    @Pointcut(value = "@annotation(com.ant.product.filter.MyAnnotation)")
    public void pointcut(){

    }

    //前置通知
    @Before("pointcut()")
    public void beforePrint(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        log.info("=======前置通知执行====方法名：" + method.getName());
//        Object[] args = joinPoint.getArgs();
//        HttpServletRequest request = (HttpServletRequest )args[1];
        log.info(Arrays.toString(joinPoint.getArgs()));
    }

    @After("pointcut()")
    public void afterPrint(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        log.info("=======后置通知执行====方法名：" + method.getName());
    }

    @Around("pointcut()")
    public Object aroundPrint(ProceedingJoinPoint joinPoint) throws Throwable{
        log.info("=======环绕通知开始=======");
        Object proceed = joinPoint.proceed();
        log.info("=======退出环绕=======");
        return proceed;
    }

    @AfterReturning(value = "pointcut()", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("注解方式AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: " + method.getName() + ", 返回结果为: " + result.toString());
    }
}
